var api = require('../../config/api.js');
var util = require('../../utils/utils.js');

Page({
  data: {
    customerCode:'',
    footer: {},
    topInfo: {
      'customerName': '暂无数据',
      'deviceNum': 0,
      'offLineNum': 0,
      'onLineNum': 0,
      'onNormalNum': 0,
      'onFaultNum': 0,
    },
    getTopInfoDate: '',
    deviceList: [],
    mPageNum: 1,
    isLogin: false,
    cusTitle: '午羊智慧电力在线检测运维系统',
    statusBarHeight: 0,
    toBarHeight: 44,
  },

  onLoad: function () {
    let sysinfo = wx.getSystemInfoSync();
    this.setData({
      statusBarHeight: sysinfo.statusBarHeight
    })

    this.getfooter()
    this.doHttpTopInfo()
    this.doHttpGetDeviceList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.popup = this.selectComponent("#popup")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    var that = this
    that.setData({
      mPageNum: 1
    })
    that.doHttpTopInfo()
    that.doHttpGetDeviceList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    var that = this
    let num = that.data.mPageNum
    that.setData({
      mPageNum: num += 1
    })
    that.doHttpGetDeviceList()
  },

  /**
   * 初始化底部Tab数据
   */
  getfooter: function () {
    var that = this
    wx.getStorage({
        key: 'psCustomerCode',
        success: (RES) => {
            that.setData({
                customerCode: RES.data
            });
            console.log('customerCode为', RES.data)
            wx.request({
                url: api.findUnread,
                method: 'POST',
                data: {
                    psCustomerCode: that.data.customerCode,
                },
                success: function (res) {
                    if (res.data.code == 200) {
                        that.setData({
                            unreadNum: res.data.data.unreadNum,
                        }),
                        console.log("未读消息数量:", that.data.unreadNum)
                        // var t = this;
                        var footerList = {
                            footerTab: 0,
                            footerlist: [{
                                diy_name: "设备概览",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/index/index"
                            }, {
                                diy_name: "实时监测",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexMonitor/indexMonitor",
                                unreadNum: that.data.unreadNum
                            }, {
                                diy_name: "",
                                color: '#221915',
                                img: "https://www.cqwuyang.cn/images/Recognition.png",
                                selimg: "https://www.cqwuyang.cn/images/Recognition.png",
                                link: ""
                            }, {
                                diy_name: "上报总览",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexSummary/indexSummary"
                            }, {
                                diy_name: "个人中心",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexMy/indexMy"
                            }]
                        }
                        that.setData({
                            footer: footerList
                        })
                    }

                }
            })
        },
    });



},
  /**
   * 获取顶部数据
   */
  doHttpTopInfo: function () { 
      var that = this
    wx.getStorage({
        key: 'psCustomerCode',
        success: (RES) => {
            that.setData({
                customerCode: RES.data
            });
            console.log('customerCode为', RES.data)
            wx.request({
              url: api.getIndexStatistics,
              method: 'POST',
              data: {
                customerCode: that.data.customerCode
              },
              success: function (res) {
                if (res.data.code == 200) {
                  that.setData({
                    topInfo: res.data.data,
                    getTopInfoDate: util.formatTime(new Date())
                  })
                }
              }
            })
        },
      });
    
  },

  /**
   * 获取设备列表
   */
  doHttpGetDeviceList: function () {
    var that = this
    wx.getStorage({
        key: 'psCustomerCode',
        success: (RES) => {
            that.setData({
                customerCode: RES.data
            });
            console.log('customerCode为', RES.data)
            wx.request({
              url: api.getDeviceList,
              method: 'POST',
              data: {
                customerCode: that.data.customerCode,
                pageNum: that.data.mPageNum,
                pageSize: 5
              },
              success: function (res) {
                if (that.data.mPageNum != 1) {
                  that.setData({
                    deviceList: that.data.deviceList.concat(res.data.data.rows)
                  })
                } else {
                  wx.stopPullDownRefresh()
                  that.setData({
                    deviceList: res.data.data.rows
                  })
                }
              }
            })
        },
      });
    
  },

  /**
   * 底部上报弹窗
   */
  changeRange: function () {
    this.popup.changeRange()
  },

  /**
   * 跳转设备详情
   */
  toDerviceDetails: function (e) {
    var derviceInfo = JSON.stringify(e.currentTarget.dataset.idtype)
    wx.navigateTo({
      url: "/pages/derviceDetails/derviceDetails?derviceInfo=" + derviceInfo
    })
  }
})